<?php

class FwUser_FwUserController extends Fw_Main
{
	public function init()
	{
		$this->validateUser();
	}

	public function listAction()
	{
		$_model = $this->_getModel('FwUser');
		$list_user = $_model->getAll();
		$this->view->list_user = $list_user;
		$this->view->rowcount  = count($list_user);
	}
	
	public function addAction()
	{
		if($this->_request->isPost()){
			$formData = $this->_request->getPost('add');
			if($formData) {
				$data = array(
							  'username'      => $this->_request->getPost('username'),
							  'name'          => $this->_request->getPost('name'),
							  'email'         => $this->_request->getPost('email'),
							  'passwd'        => md5($this->_request->getPost('passwd')),
							  'user_type'     => $this->_request->getPost('user_type'),
							  'date_register' => date('Y-m-d H:i:s'),
							  'user_status'   => $this->_request->getPost('user_status')
							  );
				$_model = $this->_getModel('FwUser');
				$add_user = $_model->addRow($data);
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function editAction()
	{
		$id = (int)$this->_request->getParam('user_id');
		$_model = $this->_getModel('FwUser');
		$edit_user = $_model->getRow($id);
		$this->view->edit_user = $edit_user;
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
			$passwd = $this->_request->getPost('passwd');
			if ($formData) {
				$data = array(
							 	'username'    => $this->_request->getPost('username'),
							 	'name'        => $this->_request->getPost('name'),
							 	'email'       => $this->_request->getPost('email'),
								'user_type'   => $this->_request->getPost('user_type'),
							 	'user_status' => $this->_request->getPost('user_status')
							 );
				if($passwd) { $data['passwd'] = md5($passwd); }
				$update_user = $_model->updateRow($id, $data);
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function deleteAction()
	{
		$id = (int)$this->_request->getParam('user_id');
		$_model = $this->_getModel('FwUser');
		$delete_user = $_model->deleteRow($id);
		return $this->_helper->redirector('list');
	}
}